package com.fary.configweb;

import com.fary.converters.MyConverterFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.stereotype.Component;

@Component
public class ConvertersConfig implements ApplicationContextAware, SmartInitializingSingleton {

    private ApplicationContext applicationContext;

    @Override
    public void afterSingletonsInstantiated() {
        GenericConversionService genericConversionService = applicationContext.getBean(GenericConversionService.class);
        genericConversionService.addConverterFactory(new MyConverterFactory());
        System.out.println("类型转换已加入！");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}